package com.xmkj.bearuser.pk;

import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.TextView;

import com.common.adapter.helper.IRecyclerViewHelper;
import com.common.banner.CBViewHolderCreator;
import com.common.banner.ConvenientBanner;
import com.common.base.Config;
import com.common.cons.Constans;
import com.common.mvp.BaseMvpActivity;
import com.common.mvp.BasePresenter;
import com.common.retrofit.bean.GoodsDeatailsBean;
import com.common.retrofit.bearusermethod.CollectionMethods;
import com.common.retrofit.bearusermethod.GoodsDetailsMethods;
import com.common.retrofit.method.ShareSuccessMethods;
import com.common.retrofit.subscriber.CommonSubscriber;
import com.common.retrofit.subscriber.SubscriberListener;
import com.common.utils.EmptyUtils;
import com.common.utils.SPUtils;
import com.common.utils.SizeUtils;
import com.common.widget.DividerDecoration.MarginDecoration;
import com.common.widget.navigation.WidgetButton;
import com.common.widget.recyclerview.refresh.recycleview.XRecyclerView;
import com.xmkj.bearuser.R;
import com.xmkj.bearuser.home.NetworkImageHolderView;
import com.xmkj.payandlogin.ShareConfig;
import com.xmkj.payandlogin.ShareManager;
import com.xmkj.payandlogin.ShareUtil;
import com.xmkj.payandlogin.share.ShareListener;
import com.xmkj.payandlogin.share.SharePlatform;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Create by leo
 * 创建日期：2018/6/27
 * 描述：
 */

public class GoodsDetailsActivity extends BaseMvpActivity {
    public static final String ID = "id";
    public static final String TYPE = "type";
    @BindView(R.id.home_view_pager)
    ConvenientBanner homeViewPager;
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.tv_xiaoliang)
    TextView tvXiaoliang;
    @BindView(R.id.tv_price)
    TextView tvPrice;
    @BindView(R.id.recyclerview_img)
    XRecyclerView recyclerview_img;
    @BindView(R.id.tv_collection)
    TextView tvCollection;
    private String type;
    private String id;
    private GoodsDeatailsBean goodsDeatailsBean;
    private List<String> viewPagerUrl = new ArrayList<>();
    private int[] page_indicatorId;
    private boolean isCollection;

    @Override
    protected BasePresenter createPresenterInstance() {
        return null;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_goods_details;
    }

    @Override
    protected void onViewCreated() {
        setRightImg();
    }

    private void setRightImg() {
        m_navigationBar.setVisibility(View.VISIBLE);
        WidgetButton widgetButton = new WidgetButton(this, getResources().getDrawable(R.mipmap.bear_share));

        widgetButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                gotoShare();
            }
        });
        m_navigationBar.setRightMenu(widgetButton);
    }

    private void gotoShare() {
        showShareButton();
        setOnShareClick(new OnShareClick() {
            @Override
            public void wechat() {
                if (ShareUtil.isInstalled(SharePlatform.WX, context)) {

//                    Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), R.mipmap.icon);
//                    if (EmptyUtils.isEmpty(mineBean.img)) {
//                        ShareUtil.shareMedia(context, SharePlatform.WX, mineBean.title, mineBean.content, mineBean.url, bitmap, shareListener);
//
//                    } else {
//                        ShareUtil.shareMedia(context, SharePlatform.WX, mineBean.title, mineBean.content, mineBean.url, mineBean.img, shareListener);
                    ShareUtil.shareMedia(context, SharePlatform.WX, goodsDeatailsBean.share.title.equals("") ? "贝儿凯莉" : goodsDeatailsBean.share.title, goodsDeatailsBean.share.content, goodsDeatailsBean.share.share_url, goodsDeatailsBean.share.pic_url, shareListener);
//                    }

                } else {
                    showToastMsg("未安装微信");
                }
            }

            @Override
            public void wethatCircle() {

            }

            @Override
            public void qq() {

            }
        });
    }
    private ShareListener shareListener = new ShareListener() {
        @Override
        public void shareSuccess() {
            showToastMsg("分享成功");
            Sharesuccessfully();
        }

        @Override
        public void shareFailure(Exception e) {
            showToastMsg("分享失败");
        }

        @Override
        public void shareCancel() {
            showToastMsg("取消分享");
        }
    };
    private void Sharesuccessfully() {
        CommonSubscriber<Object> subscriber = new CommonSubscriber<>(new SubscriberListener() {
            @Override
            public void onNext(Object o) {
                dismissProgressDialog();

//                gotoHttpRep();
            }

            @Override
            public void onError(String e, int code) {
                dismissProgressDialog();
                showToastMsg(e);
            }
        });
        ShareSuccessMethods.getInstance().shareSuccess(subscriber, SPUtils.getShareInt(Constans.UID),SPUtils.getShareString(Constans.HASHID));
        rxManager.add(subscriber);
    }
    @Override
    protected void doLogicFunc() {
        ShareConfig config = ShareConfig.instance()
                .qqId(Config.APP_QQ_ID)
                .wxSecret(Config.APP_SERECET)
                .wxId(Config.APP_ID);
        ShareManager.init(config);
        attachClickListener(tvCollection);
        if (EmptyUtils.isNotEmpty(id)) {
            gotoHttp();
        }
    }

    @Override
    protected void onViewClicked(View view) {
        if(view.getId()==tvCollection.getId()){
            if(isCollection){ //已经收藏了
                goCollection("2");
            }else {
                goCollection("1");
            }
        }
    }

    private void goCollection(final String types) {
        showProgressingDialog();
        CommonSubscriber<Object> subscriber = new CommonSubscriber<>(new SubscriberListener() {
            @Override
            public void onNext(Object o) {
                dismissProgressDialog();
                    if(types.equals("2")){
                        tvCollection.setText("收藏");
                        isCollection = false;
                    }else {
                        tvCollection.setText("已收藏");
                        isCollection = true;
                    }

            }

            @Override
            public void onError(String e, int code) {
                dismissProgressDialog();
                showToastMsg(e);
            }
        });

        CollectionMethods.getInstance().goodCollection(subscriber, SPUtils.getShareInt(Constans.UID), SPUtils.getShareString(Constans.HASHID), goodsDeatailsBean.gid,types);
        rxManager.add(subscriber);
    }

    private void gotoHttp() {
        CommonSubscriber<GoodsDeatailsBean> subscriber = new CommonSubscriber<>(new SubscriberListener() {
            @Override
            public void onNext(Object o) {
                goodsDeatailsBean = (GoodsDeatailsBean) o;
                setBanner();
                tvName.setText(goodsDeatailsBean.goods_name);
                tvPrice.setText("价格："+goodsDeatailsBean.goods_price);
                tvXiaoliang.setText(goodsDeatailsBean.sales_volume);
                if(goodsDeatailsBean.collection_state==2){ //没有收藏
                    tvCollection.setText("收藏");
                }else {
                    tvCollection.setText("已收藏");
                }
                isCollection = goodsDeatailsBean.collection_state==1;
                if(EmptyUtils.isNotEmpty(goodsDeatailsBean.goods_attribute)&&goodsDeatailsBean.goods_attribute.size()>0){

                    setRecyclerViewImg(goodsDeatailsBean.goods_attribute);
//                    ImageLoaderUtils.displayAllImg(ivImg,goodsDeatailsBean.goods_attribute);
                }

            }

            @Override
            public void onError(String e, int code) {
                dismissProgressDialog();
                showToastMsg(e);
            }
        });

        GoodsDetailsMethods.getInstance().goodInfo(subscriber, SPUtils.getShareInt(Constans.UID), SPUtils.getShareString(Constans.HASHID), id);
        rxManager.add(subscriber);
    }
    private void setRecyclerViewImg(List<String> goods_attribute) {
        IRecyclerViewHelper.init().setRecycleLineLayout(context, LinearLayoutManager.VERTICAL, recyclerview_img);
        recyclerview_img.addItemDecoration(new MarginDecoration(LinearLayoutManager.VERTICAL, (int) SizeUtils.dp2px(context, 1)));
        recyclerview_img.setHasFixedSize(true);
        recyclerview_img.setRefreshing(false);
        recyclerview_img.setRefreshEnabled(false);
        recyclerview_img.setLoadMoreEnabled(false);
        ImageAdapter      adapter = new ImageAdapter(context, goods_attribute);
//        adapter.setOnItemClickSeclect(this);
        recyclerview_img.setAdapter(adapter);

    }
    private void setBanner() {
//        viewPagerUrl.add("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1525427891333&di=32991d497454624b5c490151a185a2c1&imgtype=0&src=http%3A%2F%2Fimg.zcool.cn%2Fcommunity%2F0142135541fe180000019ae9b8cf86.jpg%401280w_1l_2o_100sh.png");
//        viewPagerUrl.add("http://img07.tooopen.com/images/20170316/tooopen_sy_201956178977.jpg");
//        viewPagerUrl.add("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1525427891333&di=32991d497454624b5c490151a185a2c1&imgtype=0&src=http%3A%2F%2Fimg.zcool.cn%2Fcommunity%2F0142135541fe180000019ae9b8cf86.jpg%401280w_1l_2o_100sh.png");
        if (viewPagerUrl.size() == 0) {
            if (goodsDeatailsBean != null && goodsDeatailsBean.goods_album.size() > 0) {
                for (int i = 0; i < goodsDeatailsBean.goods_album.size(); i++) {
                    viewPagerUrl.add(goodsDeatailsBean.goods_album.get(i));
                }
            }
        }

        page_indicatorId = new int[]{R.drawable.ic_page_indicator, R.drawable.ic_page_indicator_focused};
        homeViewPager.setPageIndicator(page_indicatorId);
        homeViewPager.setPages(new CBViewHolderCreator<NetworkImageHolderView>() {
            @Override
            public NetworkImageHolderView createHolder() {
                return new NetworkImageHolderView();
            }
        }, viewPagerUrl);
        homeViewPager.startTurning(5000);
    }
    @Override
    protected void setNavigation() {
        type = getIntent().getStringExtra(TYPE);
        id = getIntent().getStringExtra(ID);
        if (EmptyUtils.isNotEmpty(type)) {
            if (type.equals("1")) {
                setNavigationBack("新款到店");

            } else if (type.equals("2")) {
                setNavigationBack("会员最爱");

            }else {
                setNavigationBack(type);
            }

        }
    }


    @Override
    public void bindKnife() {
        ButterKnife.bind(this);
    }
}
